package cn.tworice.admin.web;

import cn.tworice.admin.service.LogService;
import cn.tworice.admin.service.RoleService;
import cn.tworice.common.entity.Admin;
import cn.tworice.common.entity.Role;
import cn.tworice.common.entity.RoleResources;
import cn.tworice.common.vo.RequestResult;
import cn.tworice.common.vo.StatusCode;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 角色管理
 */
@RestController
@RequestMapping("admin/system/role")
@Slf4j
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private LogService logService;

    @GetMapping("list")
    @ApiOperation("获取角色列表")
    public RequestResult roleList() {
        RequestResult result = new RequestResult();
        List<Role> list = roleService.getRoleList();
        result.setStatus(new StatusCode(200, "查询成功"));
        result.appendData("roleList", list);
        return result;
    }

    @GetMapping("adminList")
    @ApiOperation("通过角色ID获取人员列表")
    public RequestResult adminList(Integer roleId) {
        RequestResult result = new RequestResult();
        List<Admin> list = roleService.getAdminListByRoleId(roleId);
        result.setStatus(new StatusCode(200, "查询成功"));
        result.appendData("adminList", list);
        return result;
    }

    @PutMapping("adminToRole")
    @ApiOperation("添加人员到指定角色，可多人员")
    public RequestResult addAdminToRole(String[] adminId, Integer roleId, HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"), "添加人员-" + Arrays.toString(adminId) + "-到角色-" + roleId);
        int i = roleService.addAdminToRole(adminId, roleId);

        if (i == 1) {
            return new RequestResult(new StatusCode(200, "添加成功"));
        } else {
            return new RequestResult(new StatusCode(304, "请勿重复添加人员到角色"));
        }
    }

    @DeleteMapping("rmAdminToRole")
    @ApiOperation("从角色移除人员")
    public RequestResult rmAdminToRole(String[] adminIds, Integer roleId, HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"), "从角色-" + roleId + "-中移除人员" + Arrays.toString(adminIds));

        roleService.delAdminToRole(adminIds, roleId);
        return new RequestResult(new StatusCode(200, "操作成功"));
    }

    @GetMapping("getNoRoleAdmin")
    @ApiOperation("获取不是该角色的人员")
    public RequestResult getNoRoleAdmin(Integer roleId) {
        RequestResult result = new RequestResult();
        List<Admin> list = roleService.getNoRoleAdmin(roleId);
        result.setStatus(new StatusCode(200, "查询成功"));
        result.appendData("result", list);
        return result;
    }

    @GetMapping("getAdminByStatus")
    @ApiOperation("根据账号状态获取人员列表，0-正常，1-非正常")
    public RequestResult getAdminAll(Integer status) {
        RequestResult result = new RequestResult();
        List<Admin> list = roleService.getAdminByStatus(status);
        result.setStatus(new StatusCode(200, "获取成功"));
        result.appendData("result", list);
        return result;
    }

    @GetMapping("getResourcesByRoleId")
    @ApiOperation("根据角色ID查询资源 关联数据")
    public RequestResult getResourcesByRoleId(Integer roleId) {
        RequestResult result = new RequestResult();
        List<RoleResources> list = roleService.getResourcesByRoleId(roleId);
        result.setStatus(new StatusCode(200, "查询成功"));
        result.appendData("result", list);
        return result;
    }

    /**
     * 获取角色权限列表 暂无更新
     *
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/8 20:13
     */
    @GetMapping("resourcesByRole")
    @ApiOperation("根据角色ID查询资源 关联数据")
    public RequestResult resourcesByRole(Integer roleId) {
        RequestResult result = new RequestResult();
        List<RoleResources> list = roleService.getResourcesByRoleId(roleId);
        result.setStatus(new StatusCode(200, "查询成功"));
        result.appendData("result", list);
        return result;
    }

    /**
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/6 22:29
     */
    @PutMapping("updateRoleResources")
    @ApiOperation("更新角色权限")
    public RequestResult updateRoleResources(@ApiParam("原有资源ID数组") Integer[] haveResources, @ApiParam("更新后的资源ID数组") Integer[] updateResources, Integer roleId, HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"), "更新角色-" + roleId + "-权限为" + Arrays.toString(updateResources));


        RequestResult result = new RequestResult();
        Boolean b = roleService.updateRoleResources(haveResources, updateResources, roleId);
        if (b) {
            result.setStatus(new StatusCode(200, "更新成功"));
        }
        return result;
    }

    /**
     * 更新角色权限 叶子节点版
     *
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/8 20:02
     */
    @PutMapping("updateResources")
    @ApiOperation("更新角色权限")
    public RequestResult updateResources(HttpServletRequest request, @ApiParam("角色ID") Integer roleId, @ApiParam("更新后的资源ID数组") Integer[] resourcesId) {
        logService.addAdminLog(request.getHeader("adminID"), "更新角色-" + roleId + "-权限为" + Arrays.toString(resourcesId));

        RequestResult result = new RequestResult();
        Boolean b = roleService.setRoleResources(roleId, resourcesId);
        if (b) {
            result.setStatus(new StatusCode(200, "更新成功"));
        }
        return result;
    }

    @DeleteMapping("delRole")
    @ApiOperation("删除角色")
    public RequestResult delRole(Integer roleId, HttpServletRequest request) {

        logService.addAdminLog(request.getHeader("adminID"), "删除角色-" + roleId);

        RequestResult result = new RequestResult();
        int i = roleService.delRole(roleId);
        if (i == 1) {
            result.setStatus(new StatusCode(200, "删除成功"));
            List<Role> list = roleService.getRoleList();
            result.appendData("roleList", list);
        }
        return result;
    }

    @PutMapping("editRole")
    @ApiOperation("编辑角色")
    public RequestResult editRole(Integer roleId, String roleName, HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"), "编辑角色ID-" + roleId);

        RequestResult result = new RequestResult();
        int i = roleService.editRole(roleId, roleName);
        if (i == 1) {
            result.setStatus(new StatusCode(200, "操作成功"));
            List<Role> list = roleService.getRoleList();
            result.appendData("roleList", list);
        }
        return result;
    }
}
